package p2p.view.components
{
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.controls.HorizontalList;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.ClassFactory;
	import mx.events.FlexEvent;
	
	import p2p.model.vo.ClientVO;
	import p2p.view.renderers.ClientItemRenderer;

	[Event(name="selectedClientChange", type="flash.events.Event")]
	public class ClientsVideoList extends HorizontalList
	{
		
		public static const SELECTED_CLIENT_CHANGE:String = "selectedClientChange";
		
		public function ClientsVideoList()
		{
			itemRenderer = new ClassFactory(ClientItemRenderer);
			addEventListener( Event.CHANGE, changeHandler );
			addEventListener( FlexEvent.VALUE_COMMIT, changeHandler );
		}
		
		/**
		 * 
		 * @return Point with global coordinate for selected item renderer. 
		 * 
		 */		
		public function getSelectedRendererCoordinate() : Point
		{
			var result:Point;
			if (selectedItem)
			{
				var renderer:IListItemRenderer = itemToItemRenderer( selectedItem );
				result = new Point(renderer.x, renderer.y);
				result = listContent.localToGlobal(result);
			}
			
			return result;
		}
		
		[Bindable("selectedClientChange")]
		public function get selectedClient():ClientVO
		{
			return _selectedClient;
		}
		
		override protected function measure():void
		{
			super.measure();
			measuredMinHeight += 15;
			measuredHeight += 15;
			var length:uint = dataProvider ? dataProvider['length'] : 0;
			measuredMinWidth = measuredWidth = length * columnWidth;
		}
		
		private function changeHandler( event:Event ) : void
		{
			if ( _selectedClient != selectedItem )
			{
				_selectedClient = selectedItem as ClientVO;
				invalidateList();
				dispatchEvent( new Event(SELECTED_CLIENT_CHANGE));
			}
		}
		
		private var _selectedClient:ClientVO;
		
	}
}